﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Base.DAL;
using System.Configuration;

namespace Base.BLL
{
    /// <summary>
    /// 系统配置
    /// </summary>
    public class ConfigService
    {
        private static bool changed = true;
        /// <summary>
        /// 报警相关参数配置，键为视图名
        /// </summary>
        private static Dictionary<string,IEnumerable<IEntity>> iniWarn = null;

        private static Configuration config = null;

        #region 配置加载、配置文件读写
        /// <summary>
        /// 载入各项数据库参数配置
        /// </summary>
        public static void Load()
        {
            if (!changed)
            {
                return;
            }
            iniWarn = new Dictionary<string, IEnumerable<IEntity>>();
            iniWarn.Add("View_ZC_INI", Db.Query<View_ZC_INI>());
            iniWarn.Add("View_DBLC_INI", Db.Query<View_DBLC_INI>());
            iniWarn.Add("View_MG_INI", Db.Query<View_MG_INI>());
            iniWarn.Add("View_CQYL_INI", Db.Query<View_CQYL_INI>());
            changed = false;
        }
        /// <summary>
        /// 从文件中加载配置
        /// </summary>
        /// <param name="key">配置名</param>
        /// <param name="def">默认值</param>
        /// <returns>返回配置，如果不存在则返回默认值</returns>
        public static string GetIni(string key,string def)
        {
            if (config == null)
            {
                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            }
            if (config.AppSettings.Settings[key] == null)
                return def;
            else
                return config.AppSettings.Settings[key].Value;
        }
        /// <summary>
        /// 设置文件配置
        /// </summary>
        /// <param name="key">配置名</param>
        /// <param name="value">配置值</param>
        public static void SetIni(string key, string value)
        {
            if (config == null)
            {
                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            }
            if (config.AppSettings.Settings[key] == null)
            {
                config.AppSettings.Settings.Add(key, value);
            }
            else
            {
                config.AppSettings.Settings[key].Value = value;
            }
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        #endregion

        #region 查找报警配置
        /// <summary>
        /// 查找报警配置
        /// </summary>
        /// <typeparam name="T">数据库实体类</typeparam>
        /// <param name="cqbh">测区编号</param>
        /// <param name="bh">压力分站/传感器编号</param>
        /// <param name="rst">报警配置输出对象</param>
        /// <returns>配置是否存在</returns>
        public static bool FindWarn<T>(int cqbh, int bh, out T rst)
        {
            string iniName = typeof(T).Name;
            if (!iniWarn.ContainsKey(iniName))
            {
                rst = default(T);
                return false;
            }
            foreach (var cs in iniWarn[iniName])
            {
                int _cqbh = int.Parse(cs.GetType().GetProperty("CQBH").GetValue(cs, null).ToString());
                int _bh = int.Parse(cs.GetType().GetProperty("BH").GetValue(cs, null).ToString());
                if (cqbh == _cqbh && bh == _bh)
                {
                    rst = (T)cs;
                    return true;
                }
            }
            rst = default(T);
            return false;
        }
        #endregion

        #region 矿井、测区、工作面、巷道配置
        /// <summary>
        /// 获取矿井配置
        /// </summary>
        /// <returns>矿井配置</returns>
        public static SYSTEMKMB GetKM()
        {
            return Db.Find<SYSTEMKMB>();
        }
        /// <summary>
        /// 保存矿井配置
        /// </summary>
        /// <param name="kmb">矿井配置</param>
        /// <returns>是否保存成功</returns>
        public static bool SetKM(SYSTEMKMB kmb)
        {
            return Db.Update(kmb)>0;
        }
        /// <summary>
        /// 获取测区列表
        /// </summary>
        /// <param name="onlyActive">是否返回仅活动的测区</param>
        /// <returns>测区列表</returns>
        public static IEnumerable<SYSTEMCQB> GetCQ(bool onlyActive = false)
        {
            return onlyActive ? Db.QueryWhere<SYSTEMCQB>("ZT=1") : Db.QueryWhere<SYSTEMCQB>();
        }
        /// <summary>
        /// 设置测区，需自行判断测区名是否重复
        /// </summary>
        /// <param name="cqb">测区配置</param>
        /// <returns>是否设置成功</returns>
        public static bool SetCQ(SYSTEMCQB cqb)
        {            
            return Db.Update(cqb) > 0;
        }
        /// <summary>
        /// 添加测区，需自行判断测区名是否重复
        /// </summary>
        /// <param name="cqb">测区配置</param>
        /// <returns>是否添加成功</returns>
        public static bool AddCQ(SYSTEMCQB cqb)
        {
            return Db.Insert(cqb) > 0;
        }
        /// <summary>
        /// 获取串口配置
        /// </summary>
        /// <param name="com">串口名</param>
        /// <returns>串口配置</returns>
        public static SYSTEMCOMB GetCOM(string com)
        {
            return Db.Find<SYSTEMCOMB>(new { COM=com});
        }
        /// <summary>
        /// 设置串口
        /// </summary>
        /// <param name="com"></param>
        /// <returns></returns>
        public static bool SetCOM(SYSTEMCOMB com)
        {
            return Db.Update(com)>0;
        }
        /// <summary>
        /// 获取工作面列表
        /// </summary>
        /// <param name="onlyActive">是否返回仅活动的工作面</param>
        /// <returns>测区列表</returns>
        public static IEnumerable<View_SYSTEMGZMB> GetGZM(bool onlyActive = false)
        {
            return onlyActive ? Db.QueryWhere<View_SYSTEMGZMB>("ZT=1") : Db.QueryWhere<View_SYSTEMGZMB>();
        }
        /// <summary>
        /// 获取测区下的可用工作面列表
        /// </summary>
        /// <param name="cqbh">测区编号</param>
        /// <returns>测区列表</returns>
        public static IEnumerable<SYSTEMGZMB> GetGZM(int cqbh)
        {
            return Db.QueryWhere<SYSTEMGZMB>("CQBH="+cqbh+" AND ZT=1");
        }
        /// <summary>
        /// 设置工作面，需自行判断工作面名称是否重复
        /// </summary>
        /// <param name="gzm">工作面配置</param>
        /// <returns>是否设置成功</returns>
        public static bool SetGZM(SYSTEMGZMB gzm)
        {
            return Db.Update(gzm) > 0;
        }
        /// <summary>
        /// 添加工作面，需自行判断工作面名称是否重复
        /// </summary>
        /// <param name="gzm">测区配置</param>
        /// <returns>是否添加成功</returns>
        public static bool AddGZM(SYSTEMGZMB gzm)
        {
            return Db.Insert(gzm) > 0;
        }
        /// <summary>
        /// 获取巷道列表
        /// </summary>
        /// <param name="onlyActive">是否返回仅活动的巷道</param>
        /// <returns>巷道列表</returns>
        public static IEnumerable<View_SYSTEMXDB> GetXD(bool onlyActive = false)
        {
            return onlyActive ? Db.QueryWhere<View_SYSTEMXDB>("ZT=1") : Db.QueryWhere<View_SYSTEMXDB>();
        }
        /// <summary>
        /// 获取测区下的可用巷道列表
        /// </summary>
        /// <param name="cqbh">测区编号</param>
        /// <returns>巷道列表</returns>
        public static IEnumerable<SYSTEMXDB> GetXD(int cqbh)
        {
            return Db.QueryWhere<SYSTEMXDB>("CQBH=" + cqbh + " AND ZT=1");
        }
        /// <summary>
        /// 设置巷道，需自行判断巷道名称是否重复
        /// </summary>
        /// <param name="gzm">巷道配置</param>
        /// <returns>是否设置成功</returns>
        public static bool SetXD(SYSTEMXDB xd)
        {
            return Db.Update(xd) > 0;
        }
        /// <summary>
        /// 添加巷道，需自行判断巷道名称是否重复
        /// </summary>
        /// <param name="gzm">巷道配置</param>
        /// <returns>是否添加成功</returns>
        public static bool AddXD(SYSTEMXDB xd)
        {
            return Db.Insert(xd) > 0;
        }
        #endregion

        #region 综采配置
        /// <summary>
        /// 获取已配置综采参数的工作面列表
        /// </summary>
        /// <returns>含综采参数的工作面列表</returns>
        public static IEnumerable<View_ZC_INI_CS> GetZc()
        {
            return Db.Query<View_ZC_INI_CS>();
        }
        /// <summary>
        /// 获取指定测区、工作面的综采参数
        /// </summary>
        /// <param name="cqbh">测区编号</param>
        /// <param name="gzmbh">工作面编号</param>
        /// <returns>工作面参数</returns>
        public static View_ZC_INI_CS FindZc(int cqbh,int gzmbh)
        {
            return Db.Find<View_ZC_INI_CS>("SELECT * FROM View_ZC_INI_CS WHERE CQBH=@cqbh AND GZMBH=@gzmbh"
                ,new { cqbh=cqbh,gzmbh=gzmbh});
        }
        /// <summary>
        /// 获取压力分站位置列表
        /// </summary>
        /// <param name="gzmbh">工作面编号</param>
        /// <returns>压力分站位置列表</returns>
        public static IEnumerable<ZCYLFJWZB> GetZcWz(int gzmbh)
        {
            return Db.QueryWhere<ZCYLFJWZB>("GZMBH="+gzmbh+ " ORDER BY YLFJBH");
        }
        /// <summary>
        /// 获取可用压力分站位置列表
        /// </summary>
        /// <param name="gzmbh">工作面编号</param>
        /// <returns>压力分站位置列表</returns>
        public static IEnumerable<ZCYLFJWZB> GetZcWzEnabled(int gzmbh)
        {
            string sql = @"SELECT ZCYLFJWZB.*
                            FROM SYSTEMCQB INNER JOIN SYSTEMGZMB ON SYSTEMGZMB.CQBH=SYSTEMCQB.CQBH
                            INNER JOIN ZCYLFJWZB ON ZCYLFJWZB.GZMBH=SYSTEMGZMB.GZMBH
                            WHERE SYSTEMCQB.ZT=1 AND SYSTEMGZMB.ZT=1 AND ZCYLFJWZB.SYZT=1
                            AND ZCYLFJWZB.GZMBH=@gzmbh";
            return Db.Query<ZCYLFJWZB>(sql,new { gzmbh = gzmbh });
        }
        /// <summary>
        /// 保存工作面参数和压力分机位置表
        /// 只保留压力分机位置表中在起始编号、终止编号范围之间的位置，删除范围外的旧位置表
        /// 如果给定范围内还有未赋值的，则以默认值进行填充
        /// </summary>
        /// <param name="css">工作面综采参数</param>
        /// <param name="wzs">位置列表，为null时为新建</param>
        /// <returns>0:修改成功 1:添加成功 -1：操作失败</returns>
        public static int SaveZc(View_ZC_INI_CS css,IEnumerable<ZCYLFJWZB> wzs=null)
        {
            //先判断编号是否在同一测区下有重复
            int cqbh = Db.Find<int>("SELECT CQBH FROM SYSTEMGZMB WHERE GZMBH=" + css.GZMBH);
            string sql = @"GZMBH IN(SELECT GZMBH FROM SYSTEMGZMB WHERE CQBH="+cqbh+
                            ") AND GZMBH<>" + css.GZMBH + " AND YLFJBH >=" + css.YLFJQSBH + " AND YLFJBH <=" + css.YLFJZZBH;
            int count=Db.Count<ZCYLFJWZB>(sql);
            if (count > 0)
            {
                return -1;
            }
            //先删除旧列表
            Db.Execute("DELETE FROM ZCYLFJWZB WHERE GZMBH="+css.GZMBH);
            //更新列表
            List<ZCYLFJWZB> _wzs=(wzs == null?new List<ZCYLFJWZB>():wzs.ToList<ZCYLFJWZB>());            
            //删除列表中不在范围内的数据
            _wzs.RemoveAll(x => x.YLFJBH < css.YLFJQSBH || x.YLFJBH > css.YLFJZZBH);
            for (int i = css.YLFJQSBH; i <= css.YLFJZZBH; i++)
            {
                //为不在列表中但在范围内的位置赋予默认值
                var find = _wzs.Find(x => x.YLFJBH == i);
                if (find == null)
                {
                    ZCYLFJWZB wz = new ZCYLFJWZB()
                    {
                        GZMBH = css.GZMBH,
                        YLFJBH = i,
                        DYZJBH = "支架" + i,
                        SBBM = "",
                        WZ = "上部",
                        SYZT = 1
                    };
                    _wzs.Add(wz);
                }
                else
                {
                    find.GZMBH = css.GZMBH;
                }
            }
            ZCCSB cs = new ZCCSB();
            Db.Cpy(css,cs);
            //插入工作面参数值
            int rst = 0;
            if (/*wzs == null || */Db.Count<ZCCSB>("GZMBH="+cs.GZMBH)==0)
            {
                Db.Execute(Db.SqlInsert(new ZCCSB()), cs);
                rst = 1;
            }
            else
            {
                Db.Execute(Db.SqlUpdate(new ZCCSB()), cs);
            }
            //插入列表所有值
            Db.Insert<ZCYLFJWZB>(_wzs);
            return rst;
        }
        #endregion

        #region 顶板离层/下沉配置
        /// <summary>
        /// 获取已配置顶板离层/下沉参数的巷道列表
        /// </summary>
        /// <param name="isLC">传感器是否为顶板离层</param>
        /// <returns>含顶板离层/下沉参数的巷道列表</returns>
        public static IEnumerable<View_DBLC_INI_CS> GetDblc(bool isLC)
        {
            return Db.QueryWhere<View_DBLC_INI_CS>("CGQLX='"+(isLC?"LC":"XC")+"'");
        }
        /// <summary>
        /// 获取指定测区、工作面的顶板离层/下沉参数
        /// </summary>
        /// <param name="cqbh">测区编号</param>
        /// <param name="xdbh">巷道编号</param>
        /// <param name="isLC">true:离层 false:下沉</param>
        /// <returns>巷道参数</returns>
        public static View_DBLC_INI_CS FindDblc(int cqbh, int xdbh,bool isLC)
        {
            if(isLC)
                return Db.Find<View_DBLC_INI_CS>("SELECT * FROM View_DBLC_INI_CS WHERE CQBH=@cqbh AND XDBH=@xdbh AND CGQLX='LC'"
                    , new { cqbh = cqbh, xdbh = xdbh });
            else
                return Db.Find<View_DBLC_INI_CS>("SELECT * FROM View_DBLC_INI_CS WHERE CQBH=@cqbh AND XDBH=@xdbh AND CGQLX='XC'"
                    , new { cqbh = cqbh, xdbh = xdbh });
        }
        /// <summary>
        /// 获取顶板离层/下沉传感器位置列表
        /// </summary>
        /// <param name="xdbh">巷道编号</param>
        /// <param name="isLC">true:离层 false:下沉</param>
        /// <returns>顶板离层/下沉传感器位置列表</returns>
        public static IEnumerable<DBLCCGQWZB> GetDblcWz(int xdbh, bool isLC)
        {
            return Db.QueryWhere<DBLCCGQWZB>("XDBH=" + xdbh+" AND CGQLX='"+(isLC?"LC":"XC")+"' ORDER BY CGQBH");
        }
        /// <summary>
        /// 获取可用顶板离层/下沉传感器位置列表
        /// </summary>
        /// <param name="xdbh">巷道编号</param>
        /// <param name="isLC">true:离层 false:下沉</param>
        /// <returns>顶板离层/下沉传感器位置列表</returns>
        public static IEnumerable<DBLCCGQWZB> GetDblcWzEnabled(int xdbh,bool isLC)
        {
            string sql = @"SELECT DBLCCGQWZB.*
                            FROM SYSTEMCQB INNER JOIN SYSTEMXDB ON SYSTEMXDB.CQBH=SYSTEMCQB.CQBH
                            INNER JOIN DBLCCGQWZB ON DBLCCGQWZB.XDBH=SYSTEMXDB.XDBH
                            WHERE SYSTEMCQB.ZT=1 AND SYSTEMXDB.ZT=1 AND DBLCCGQWZB.SYZT=1
                            AND DBLCCGQWZB.XDBH=@xdbh AND CGQLX=@cgqlx";
            return Db.Query<DBLCCGQWZB>(sql, new { xdbh = xdbh, cgqlx=isLC?"LC":"XC" });
        }
        /// <summary>
        /// 保存巷道参数和顶板离层/下沉位置表
        /// 只保留顶板离层/下沉位置表中在起始编号、终止编号范围之间的位置，删除范围外的旧位置表
        /// 如果给定范围内还有未赋值的，则以默认值进行填充
        /// </summary>
        /// <param name="css">巷道顶板离层/下沉参数</param>
        /// <param name="wzs">位置列表，为null时为新建</param>
        /// <returns>0:修改成功 1:添加成功 -1：操作失败</returns>
        public static int SaveDblc(View_DBLC_INI_CS css, IEnumerable<DBLCCGQWZB> wzs = null)
        {
            //先判断编号是否在同一测区下有重复
            int cqbh = Db.Find<int>("SELECT CQBH FROM SYSTEMXDB WHERE XDBH=" + css.XDBH);
            //string sql = @"XDBH IN(SELECT XDBH FROM SYSTEMXDB WHERE CQBH=" + cqbh +
            //                ") AND ((XDBH<>" + css.XDBH+" AND CGQLX='"+css.CGQLX+"') OR"
            //                + "(XDBH=" + css.XDBH + " AND CGQLX<>'" + css.CGQLX + "'))"
            //                + " AND CGQBH >=" + css.CGQQSBH + " AND CGQBH <=" + css.CGQZZBH;
            string sql = @"XDBH IN(SELECT XDBH FROM SYSTEMXDB WHERE CQBH=" + cqbh +
                ") AND (XDBH<>" + css.XDBH+" OR"
                + "(XDBH=" + css.XDBH + " AND CGQLX<>'" + css.CGQLX + "'))"
                + " AND CGQBH >=" + css.CGQQSBH + " AND CGQBH <=" + css.CGQZZBH;
            int count = Db.Count<DBLCCGQWZB>(sql);
            if (count > 0)
            {
                return -1;
            }
            //先删除旧列表
            Db.Execute("DELETE FROM DBLCCGQWZB WHERE XDBH=" + css.XDBH+" AND CGQLX='"+css.CGQLX+"'");
            //更新列表
            List<DBLCCGQWZB> _wzs = (wzs == null ? new List<DBLCCGQWZB>() : wzs.ToList<DBLCCGQWZB>());
            //删除列表中不在范围内的数据
            _wzs.RemoveAll(x => x.CGQBH < css.CGQQSBH || x.CGQBH > css.CGQZZBH);
            for (int i = css.CGQQSBH; i <= css.CGQZZBH; i++)
            {
                //为不在列表中但在范围内的位置赋予默认值
                var find = _wzs.Find(x => x.CGQBH == i);
                if (find == null)
                {
                    DBLCCGQWZB wz = new DBLCCGQWZB()
                    {
                        XDBH = css.XDBH,
                        CGQBH = i,
                        CGQLX=css.CGQLX,
                        CGQWZ = "",
                        AJDSD = 0,
                        BJDSD = 0,
                        SYZT = 1
                    };
                    _wzs.Add(wz);
                }
                else
                {
                    find.XDBH = css.XDBH;
                }
            }
            DBLCCSB cs = new DBLCCSB();
            Db.Cpy(css, cs);
            //插入工作面参数值
            int rst = 0;
            if (/*wzs == null*/Db.Count<DBLCCSB>("XDBH=" + cs.XDBH+" AND CGQLX='"+cs.CGQLX+"'") == 0)
            {
                Db.Execute(Db.SqlInsert(new DBLCCSB()), cs);
                rst = 1;
            }
            else
            {
                Db.Execute(Db.SqlUpdate(new DBLCCSB()), cs);
            }
            //插入列表所有值
            Db.Insert<DBLCCGQWZB>(_wzs);
            return rst;
        }
        #endregion

        #region 锚杆/索应力配置
        /// <summary>
        /// 获取已配置锚杆/索应力参数的巷道列表
        /// </summary>
        /// <returns>含锚杆/索应力参数的巷道列表</returns>
        public static IEnumerable<View_MG_INI_CS> GetMg()
        {
            return Db.Query<View_MG_INI_CS>();
        }
        /// <summary>
        /// 获取指定测区、工作面的锚杆/索参数
        /// </summary>
        /// <param name="cqbh">测区编号</param>
        /// <param name="xdbh">巷道编号</param>
        /// <returns>巷道参数</returns>
        public static View_MG_INI_CS FindMg(int cqbh, int xdbh)
        {
            return Db.Find<View_MG_INI_CS>("SELECT * FROM View_MG_INI_CS WHERE CQBH=@cqbh AND XDBH=@xdbh"
                , new { cqbh = cqbh, xdbh = xdbh });
        }        
        /// <summary>
        /// 获取锚杆/索应力传感器位置列表
        /// </summary>
        /// <param name="xdbh">巷道编号</param>
        /// <returns>锚杆/索应力传感器位置列表</returns>
        public static IEnumerable<MGYLCGQWZB> GetMgWz(int xdbh)
        {
            return Db.QueryWhere<MGYLCGQWZB>("XDBH=" + xdbh + " ORDER BY CGQBH");
        }
        /// <summary>
        /// 获取可用锚杆/索应力传感器位置列表
        /// </summary>
        /// <param name="xdbh">巷道编号</param>
        /// <returns>锚杆/索应力传感器位置列表</returns>
        public static IEnumerable<MGYLCGQWZB> GetMgWzEnabled(int xdbh)
        {
            string sql = @"SELECT MGYLCGQWZB.*
                            FROM SYSTEMCQB INNER JOIN SYSTEMXDB ON SYSTEMXDB.CQBH=SYSTEMCQB.CQBH
                            INNER JOIN MGYLCGQWZB ON MGYLCGQWZB.XDBH=SYSTEMXDB.XDBH
                            WHERE SYSTEMCQB.ZT=1 AND SYSTEMXDB.ZT=1 AND MGYLCGQWZB.SYZT=1
                            AND MGYLCGQWZB.XDBH=@xdbh";
            return Db.Query<MGYLCGQWZB>(sql, new { xdbh = xdbh});
        }
        /// <summary>
        /// 保存巷道参数和锚杆/索应力位置表
        /// 只保留锚杆/索应力位置表中在起始编号、终止编号范围之间的位置，删除范围外的旧位置表
        /// 如果给定范围内还有未赋值的，则以默认值进行填充
        /// </summary>
        /// <param name="css">巷道锚杆/索应力参数</param>
        /// <param name="wzs">位置列表，为null时为新建</param>
        /// <returns>0:修改成功 1:添加成功 -1：操作失败</returns>
        public static int SaveMg(View_MG_INI_CS css, IEnumerable<MGYLCGQWZB> wzs = null)
        {
            //先判断编号是否在同一测区下有重复
            int cqbh = Db.Find<int>("SELECT CQBH FROM SYSTEMXDB WHERE XDBH=" + css.XDBH);
            string sql = @"XDBH IN(SELECT XDBH FROM SYSTEMXDB WHERE CQBH=" + cqbh +
                            ") AND XDBH<>" + css.XDBH + " AND CGQBH >=" + css.CGQQSBH + " AND CGQBH <=" + css.CGQZZBH;
            int count = Db.Count<MGYLCGQWZB>(sql);
            if (count > 0)
            {
                return -1;
            }
            //先删除旧列表
            Db.Execute("DELETE FROM MGYLCGQWZB WHERE XDBH=" + css.XDBH);
            //更新列表
            List<MGYLCGQWZB> _wzs = (wzs == null ? new List<MGYLCGQWZB>() : wzs.ToList<MGYLCGQWZB>());
            //删除列表中不在范围内的数据
            _wzs.RemoveAll(x => x.CGQBH < css.CGQQSBH || x.CGQBH > css.CGQZZBH);
            for (int i = css.CGQQSBH; i <= css.CGQZZBH; i++)
            {
                //为不在列表中但在范围内的位置赋予默认值
                var find = _wzs.Find(x => x.CGQBH == i);
                if (find == null)
                {
                    MGYLCGQWZB wz = new MGYLCGQWZB()
                    {
                        XDBH = css.XDBH,
                        CGQBH = i,
                        CGQLX = "MG",//MG：锚杆 MS:锚索
                        CGQWZ = "位置" + i,
                        SYZT = 1
                    };
                    _wzs.Add(wz);
                }
                else
                {
                    find.XDBH = css.XDBH;
                }
            }
            MGYLCSB cs = new MGYLCSB();
            Db.Cpy(css, cs);
            //插入工作面参数值
            int rst = 0;
            if (/*wzs == null || */Db.Count<MGYLCSB>("XDBH=" + cs.XDBH) == 0)
            {
                Db.Execute(Db.SqlInsert(new MGYLCSB()), cs);
                rst = 1;
            }
            else
            {
                Db.Execute(Db.SqlUpdate(new MGYLCSB()), cs);
            }
            //插入列表所有值
            Db.Insert<MGYLCGQWZB>(_wzs);
            return rst;
        }
        #endregion

        #region 超前应力配置
        /// <summary>
        /// 获取已配置超前应力参数的巷道列表
        /// </summary>
        /// <returns>含超前应力参数的巷道列表</returns>
        public static IEnumerable<View_CQYL_INI_CS> GetCqyl()
        {
            return Db.Query<View_CQYL_INI_CS>();
        }
        /// <summary>
        /// 获取指定测区、工作面的超前应力参数
        /// </summary>
        /// <param name="cqbh">测区编号</param>
        /// <param name="xdbh">巷道编号</param>
        /// <returns>巷道参数</returns>
        public static View_CQYL_INI_CS FindCqyl(int cqbh, int xdbh)
        {
            return Db.Find<View_CQYL_INI_CS>("SELECT * FROM View_CQYL_INI_CS WHERE CQBH=@cqbh AND XDBH=@xdbh"
                , new { cqbh = cqbh, xdbh = xdbh });
        }
        /// <summary>
        /// 获取超前应力传感器位置列表
        /// </summary>
        /// <param name="xdbh">巷道编号</param>
        /// <returns>超前应力传感器位置列表</returns>
        public static IEnumerable<CQYLCGQWZB> GetCqylWz(int xdbh)
        {
            return Db.QueryWhere<CQYLCGQWZB>("XDBH=" + xdbh + " ORDER BY CGQBH");
        }
        /// <summary>
        /// 获取可用超前应力传感器位置列表
        /// </summary>
        /// <param name="xdbh">巷道编号</param>
        /// <returns>超前应力传感器位置列表</returns>
        public static IEnumerable<CQYLCGQWZB> GetCqylWzEnabled(int xdbh)
        {
            string sql = @"SELECT CQYLCGQWZB.*
                            FROM SYSTEMCQB INNER JOIN SYSTEMXDB ON SYSTEMXDB.CQBH=SYSTEMCQB.CQBH
                            INNER JOIN CQYLCGQWZB ON CQYLCGQWZB.XDBH=SYSTEMXDB.XDBH
                            WHERE SYSTEMCQB.ZT=1 AND SYSTEMXDB.ZT=1 AND CQYLCGQWZB.SYZT=1
                            AND CQYLCGQWZB.XDBH=@xdbh";
            return Db.Query<CQYLCGQWZB>(sql, new { xdbh = xdbh });
        }
        /// <summary>
        /// 保存巷道参数和超前应力位置表
        /// 只保留超前应力位置表中在起始编号、终止编号范围之间的位置，删除范围外的旧位置表
        /// 如果给定范围内还有未赋值的，则以默认值进行填充
        /// </summary>
        /// <param name="css">巷道超前应力参数</param>
        /// <param name="wzs">位置列表，为null时为新建</param>
        /// <returns>0:修改成功 1:添加成功 -1：操作失败</returns>
        public static int SaveCqyl(View_CQYL_INI_CS css, IEnumerable<CQYLCGQWZB> wzs = null)
        {
            //先判断编号是否在同一测区下有重复
            int cqbh = Db.Find<int>("SELECT CQBH FROM SYSTEMXDB WHERE XDBH=" + css.XDBH);
            string sql = @"XDBH IN(SELECT XDBH FROM SYSTEMXDB WHERE CQBH=" + cqbh +
                            ") AND XDBH<>" + css.XDBH + " AND CGQBH >=" + css.CGQQSBH + " AND CGQBH <=" + css.CGQZZBH;
            int count = Db.Count<MGYLCGQWZB>(sql);
            if (count > 0)
            {
                return -1;
            }
            //先删除旧列表
            Db.Execute("DELETE FROM CQYLCGQWZB WHERE XDBH=" + css.XDBH);
            //更新列表
            List<CQYLCGQWZB> _wzs = (wzs == null ? new List<CQYLCGQWZB>() : wzs.ToList<CQYLCGQWZB>());
            //删除列表中不在范围内的数据
            _wzs.RemoveAll(x => x.CGQBH < css.CGQQSBH || x.CGQBH > css.CGQZZBH);
            for (int i = css.CGQQSBH; i <= css.CGQZZBH; i++)
            {
                //为不在列表中但在范围内的位置赋予默认值
                var find = _wzs.Find(x => x.CGQBH == i);
                if (find == null)
                {
                    CQYLCGQWZB wz = new CQYLCGQWZB()
                    {
                        XDBH = css.XDBH,
                        CGQBH = i,
                        CGQWZ = "位置" + i,
                        SD=8,
                        SYZT = 1
                    };
                    _wzs.Add(wz);
                }
                else
                {
                    find.XDBH = css.XDBH;
                }
            }
            CQYLCSB cs = new CQYLCSB();
            Db.Cpy(css, cs);
            //插入工作面参数值
            int rst = 0;
            if (/*wzs == null || */Db.Count<CQYLCSB>("XDBH=" + cs.XDBH) == 0)
            {
                Db.Execute(Db.SqlInsert(new CQYLCSB()), cs);
                rst = 1;
            }
            else
            {
                Db.Execute(Db.SqlUpdate(new CQYLCSB()), cs);
            }
            //插入列表所有值
            Db.Insert<CQYLCGQWZB>(_wzs);
            return rst;
        }
        #endregion
        public static void Test()
        {
            Load();
            //测试获取报警信息
            //View_DBLC_INI ini;
            //FindWarn<View_DBLC_INI>(1,1,out ini);
            var x=ConfigService.GetZcWz(1);
            //测试读取矿名、测区、工作面
            //var x = ConfigService.GetKM();
            //var x=ConfigService.GetCQ();
            //var x = ConfigService.GetGZM(1);
            //测试修改综采
            //var zcs = ConfigService.GetZc();
            //var zc = zcs.FirstOrDefault(x => x.GZMBH == 6);
            //var wz = ConfigService.GetZcWz(zc.GZMBH);
            //zc.YLFJZZBH = 12;
            //ConfigService.SaveZc(zc, wz);
            //测试添加综采
            //var newZc = zcs.ElementAt(0).Clone();
            //newZc.GZMBH = 6;
            //newZc.YLFJQSBH = 6;
            //newZc.YLFJZZBH = 10;
            //ConfigService.SaveZc(newZc, null);
            //var x = ConfigService.GetZcWz(1);
            //测试修改顶板离层/下沉
            //var dblcs = ConfigService.GetDblc();
            //var dblc = dblcs.FirstOrDefault(x => x.XDBH == 1);
            //var wzs = ConfigService.GetDblcWz(dblc.XDBH);
            //dblc.CGQZZBH = 12;
            //ConfigService.SaveZc(dblc, wzs);
            //测试添加顶板离层/下沉
            //var newDblc = dblcs.ElementAt(0).Clone();
            //newDblc.XDBH = 2;
            //newDblc.CGQQSBH = 13;
            //newDblc.CGQZZBH = 15;
            //ConfigService.SaveDblc(newDblc, null);
            //测试修改锚杆/索
            //var mgs = ConfigService.GetMg();
            //var dblc = mgs.FirstOrDefault(x => x.XDBH == 1);
            //var wzs = ConfigService.GetMgWz(dblc.XDBH);
            //dblc.CGQZZBH = 6;
            //ConfigService.SaveMg(dblc, wzs);
            //测试添加锚杆/索
            //var newMg = mgs.ElementAt(0).Clone();
            //newMg.XDBH = 2;
            //newMg.CGQQSBH = 10;
            //newMg.CGQZZBH = 15;
            //ConfigService.SaveMg(newMg, null);
            //测试修改超前应力
            //var cqyls = ConfigService.GetCqyl();
            //var cqyl = cqyls.FirstOrDefault(x => x.XDBH == 1);
            //var wzs = ConfigService.GetCqylWz(cqyl.XDBH);
            //cqyl.CGQZZBH = 6;
            //ConfigService.SaveCqyl(cqyl, wzs);
            //测试添加超前应力
            //var newCqyl = cqyls.ElementAt(0).Clone();
            //newCqyl.XDBH = 2;
            //newCqyl.CGQQSBH = 10;
            //newCqyl.CGQZZBH = 15;
            //ConfigService.SaveCqyl(newCqyl, null);
            Console.WriteLine();
        }
    }
}
